/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.DepartmentDAO;
import exatrez.manager.DepartManager;
import exatrez.manager.ObservationManager;
import exatrez.model.Department;
import exatrez.model.InfoAudit;
import exatrez.model.Observation;
import exatrez.model.ObservationSummary;
import exatrez.model.ObservationTask;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.config.AuthRequired;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author wan.fairul
 */
@UrlBinding("/adminObs")
@AuthRequired
public class ObservationAdminAction extends BaseActionBean {

    private static Logger LOG = LoggerFactory.getLogger(ObservationAdminAction.class);
    @Inject
    private ObservationManager observationManager;
    private List<Observation> list;
    private Observation observation;
    private ObservationTask observationTask;
    private String id;

    @DefaultHandler
    public Resolution showForm() {
        return new JSP("admin/list_report.jsp");
    }

    public Resolution search() {
        list = observationManager.findObsClose(observation);
        return new JSP("admin/list_report.jsp");
    }

    public Resolution view_obsTask() {
        id = getContext().getRequest().getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            observation = observationManager.getObservation(id);
        }
        return new JSP("admin/obs_task_admin.jsp");
    }

    public Resolution popupEditStatus() {
        String observationTaskId = getContext().getRequest().getParameter("observationTaskId");
        if (observationTaskId != null) {
            observationTask = observationManager.getObservationTask(observationTaskId);
        }
        return new JSP("admin/edit_obs_status.jsp").addParameter("popup", "true");
    }

    public Resolution saveEditStatus() {
        String observationTaskId = getContext().getRequest().getParameter("observationTask.observationTaskId");
        if (observationTaskId != null) {
            User user = getContext().getCurrentUser();
            ObservationTask obsTask = observationManager.getObservationTask(observationTaskId);
            InfoAudit ia = obsTask.getInfoAudit();
            obsTask.setStatusRemarks(observationTask.getStatusRemarks());
            ia.setUpdateDate(new Date());
            ia.setUpdateId(user);
            obsTask.setInfoAudit(ia);
            observationManager.saveOrUpdateTask(obsTask);
            addSimpleMessage("Success Update");
            return new RedirectResolution(ObservationAdminAction.class, "view_obsTask").addParameter("id", String.valueOf(obsTask.getObservation().getObservationId()));   
        }
        return new RedirectResolution(ObservationAdminAction.class, "view_obsTask");
        }

    public ObservationManager getObservationManager() {
        return observationManager;
    }

    public void setObservationManager(ObservationManager observationManager) {
        this.observationManager = observationManager;
    }

    public List<Observation> getList() {
        return list;
    }

    public void setList(List<Observation> list) {
        this.list = list;
    }

    public Observation getObservation() {
        return observation;
    }

    public void setObservation(Observation observation) {
        this.observation = observation;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ObservationTask getObservationTask() {
        return observationTask;
    }

    public void setObservationTask(ObservationTask observationTask) {
        this.observationTask = observationTask;
    }
}
